package com.amt.olcacon;

import com.amt.olcacon.entity.PivotTable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConsoleDisplay {
    
    /**
     * 在控制台显示透视表
     */
    public static void displayPivotTable(PivotTable pivotTable) {
        displayPivotTable(pivotTable, null);
    }
    
    /**
     * 在控制台显示透视表（使用简化的列名）
     */
    public static void displayPivotTable(PivotTable pivotTable, Map<String, String> columnNameMapping) {
        List<String> rowNames = pivotTable.getRowNames();
        List<String> columnNames = pivotTable.getColumnNames();
        
        if (rowNames.isEmpty() || columnNames.isEmpty()) {
            System.out.println("无数据可显示");
            return;
        }
        
        // 计算列宽
        int flowNameWidth = Math.max(20, 
            rowNames.stream().mapToInt(String::length).max().orElse(10) + 2);
        
        Map<String, Integer> columnWidths = calculateColumnWidths(pivotTable, columnNameMapping);
        
        // 打印表头
        printHeader(flowNameWidth, columnNames, columnWidths, columnNameMapping);
        
        // 打印分隔线
        printSeparator(flowNameWidth, columnWidths);
        
        // 打印数据行
        for (String rowName : rowNames) {
            printDataRow(rowName, pivotTable.getRow(rowName), flowNameWidth, 
                        columnNames, columnWidths);
        }
        
        // 打印统计信息
        System.out.printf("\n表格统计: %d 行 × %d 列%n", 
                         pivotTable.getRowCount(), pivotTable.getColumnCount());
    }
    
    private static Map<String, Integer> calculateColumnWidths(PivotTable pivotTable, 
                                                            Map<String, String> columnNameMapping) {
        Map<String, Integer> widths = new HashMap<>();
        List<String> columnNames = pivotTable.getColumnNames();
        
        for (String columnName : columnNames) {
            int width = 15; // 最小宽度
            
            // 考虑列名宽度
            String displayName = columnNameMapping != null ? 
                columnNameMapping.getOrDefault(columnName, columnName) : columnName;
            width = Math.max(width, displayName.length() + 2);
            
            // 考虑数据宽度
            for (String rowName : pivotTable.getRowNames()) {
                Double value = pivotTable.getValue(rowName, columnName);
                if (value != null) {
                    String formatted = DataFormatter.formatScientific(value);
                    width = Math.max(width, formatted.length() + 2);
                }
            }
            
            widths.put(columnName, width);
        }
        
        return widths;
    }
    
    private static void printHeader(int flowNameWidth, List<String> columnNames,
                                  Map<String, Integer> columnWidths, Map<String, String> columnNameMapping) {
        System.out.printf("%-" + flowNameWidth + "s", "材料/组件");
        
        for (String columnName : columnNames) {
            String displayName = columnNameMapping != null ? 
                columnNameMapping.getOrDefault(columnName, columnName) : columnName;
            int width = columnWidths.get(columnName);
            System.out.printf("%-" + width + "s", shortenText(displayName, width - 1));
        }
        System.out.println();
    }
    
    private static void printSeparator(int flowNameWidth, Map<String, Integer> columnWidths) {
        System.out.print("-".repeat(flowNameWidth));
        for (int width : columnWidths.values()) {
            System.out.print("-".repeat(width));
        }
        System.out.println();
    }
    
    private static void printDataRow(String rowName, Map<String, Double> rowData, 
                                   int flowNameWidth, List<String> columnNames,
                                   Map<String, Integer> columnWidths) {
        System.out.printf("%-" + flowNameWidth + "s", shortenText(rowName, flowNameWidth - 1));
        
        for (String columnName : columnNames) {
            Double value = rowData.get(columnName);
            String displayValue = value != null ? DataFormatter.formatScientific(value) : "N/A";
            int width = columnWidths.get(columnName);
            System.out.printf("%-" + width + "s", displayValue);
        }
        System.out.println();
    }
    
    private static String shortenText(String text, int maxLength) {
        if (text.length() <= maxLength) {
            return text;
        }
        return text.substring(0, maxLength - 3) + "...";
    }
}